package dataStructure.sort.insertionSort;

import java.util.Arrays;

/**
 * @author lhn
 * @date 2024-08-29 16:18
 */

public class InsertionSort {
    public static void main(String[] args) {
//        int[] arr = {3, 1, 5, 7, 2, 4, 9, 6, 8};
        int[] arr = {101, 34, 119, 1, -1, 90, 223};
        System.out.println("排序前：");
        System.out.println(Arrays.toString(arr));
        sort(arr);
        System.out.println("排序后：");
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {

        for (int i = 1; i < arr.length - 1; i++) {
            System.out.println("第" + i + "趟排序");
            //待插入的数
            int insertValue = arr[i];
            int insertIndex = i - 1;

            while(insertIndex >= 0 && insertValue < arr[insertIndex]) {
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex --;
            }
            if (insertIndex + 1 != i) {
                arr[insertIndex + 1] = insertValue;
            }
            System.out.println(Arrays.toString(arr));
        }

    }
}
